export declare type TargetContext = '_self' | '_blank';

/**
 * Download file according to file address
 * @param {*} sUrl
 */
export async function downloadByUrl({ url, fileName }: { url: string; fileName: string }) {
    // const response = await fetch(url, {
    //     method: 'POST',
    //     mode: 'cors',
    //     credentials: 'include',
    //     redirect: 'follow',
    // });
    // const blob = await response.blob();
    // const blobUrl = window.URL.createObjectURL(new Blob([blob]));
    // const link = document.createElement('a');
    // link.href = blobUrl;
    // link.setAttribute('download', fileName);
    // document.body.appendChild(link);
    // link.click();

    const response = await fetch(url);
    const blob = await response.blob();
    const blobUrl = window.URL.createObjectURL(new Blob([blob]));
    const link = document.createElement('a');
    link.href = blobUrl;
    link.setAttribute('download', fileName);
    document.body.appendChild(link);
    link.click();
}

/**
 * Download according to the background interface file stream
 * @param {*} data
 * @param {*} filename
 * @param {*} mime
 * @param {*} bom
 */
export function downloadByData(data: Blob, filename: string) {
    const blobURL = window.URL.createObjectURL(data);

    const tempLink = document.createElement('a');

    tempLink.style.display = 'none';
    tempLink.href = blobURL;
    tempLink.setAttribute('download', filename);
    if (typeof tempLink.download === 'undefined') {
        tempLink.setAttribute('target', '_blank');
    }
    document.body.appendChild(tempLink);
    tempLink.click();
    document.body.removeChild(tempLink);
    window.URL.revokeObjectURL(blobURL);
}
